In this article we will discuss to replace strings in JavaScript by using replace() method. It searches a given specified string and replaces with the replacement values.
For example:
If we want to replace tutorials with techniques
<script type="text/javascript">
var myString = "infinetsofttutorials";
var result = myString.replace("tutorials", "techniques");
alert(result);
</script>
Output:
Replace string with case sensitive:
A regular expression between 2 forward slashes(//) and letter g after last forward slash. This is case sensitive below example describes about it.
<script type="text/javascript">
var myString = "A Black pen is on the black table.";
var result = myString.replace(/black/g, "white");
alert(result);
</script>
Output:
Replace string with case insensitive:
Same as that of case sensitive instead of g after the forward slash here we are using gi. The is case insensitive.
<script type="text/javascript">
var myString = "ABlack pen is on the black table.";
var result = myString.replace(/black/gi, "red");
alert(result);
</script>
Output:
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article